package utilityTools;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.geotools.geojson.geom.*;
import org.json.simple.*;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.*;

public class BaseGeometryUtility {
	
	/**
	 * 将wkt字符串转换为GeoJson的字符串
	 * @param wkt wkt表示的Geometry的字符串对象
	 * @return
	 */
	public static String Wkt2GeoJson(String wkt) {
		String json = null;
		try {
			WKTReader reader = new WKTReader();
			Geometry geometry = reader.read(wkt);
			StringWriter writer = new StringWriter();
			GeometryJSON g = new GeometryJSON();
			g.write(geometry, writer);
			Map<String,Object> map=new HashMap<>();
			map.put("geometry", writer);
			json = JSONObject.toJSONString(map);
//			Polygon polygonTransed= g.readPolygon(GeoJSONUtil.toReader(json));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}
	
	/**
	 * 将GeoJson表示的Geometry字符串转换为wkt的表示形式
	 * @param geoJson 合法的GeoJson字符串对象
	 * @return
	 */
	public static String jsonToWkt(String geoJson) {
		String wkt = null;
		GeometryJSON gjson = new GeometryJSON();
		Reader reader = new StringReader(geoJson);
		try {
			Geometry geometry = gjson.read(reader);
			
			wkt = geometry.toText();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return wkt;
	}
}
